<template>
  <div id="insertPKL">
    <el-dialog :visible.sync="visible" title="选择知识库" width="800px"
               @closed='closedDialog' @open="openDialog">
      <el-row>
        <el-col :span="8">
          <div style="display: inline-block; color: red; line-height: 40px; font-size: 10px;">
            (*术语模块只同步已设置图书级别的术语)
          </div>
        </el-col>
        <el-col :span="16">
          <div style="float: right">
            <div style="display: inline-block; color: red;font-weight: bolder;">请选择知识体系:</div>
            <div style="display: inline-block">
              <el-input v-model.trim="searchInput" placeholder="请输入名称搜索">
                <el-select @change="valueChange" collapse-tags slot="prepend"
                           v-model="systemType"
                           style="width: 180px;">
                  <el-option
                    v-for="item in CONST.SYSTEM_TYPE"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-input>
            </div>
          </div>
        </el-col>
      </el-row>
      <el-table highlight-current-row ref="multipleTable"
                :data="filteredGridData.slice((currentPage-1)*pageSize,currentPage*pageSize)"
                border style="width: 100%;margin-top: 5px" stripe :height="300"
                row-key="knowledgeLibId"
                @selection-change="handleSelectionChange">
        <el-table-column
          :reserve-selection="true"
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column label="知识库名称" prop="knowledgeLibName"></el-table-column>
        <el-table-column label="是否自动推送">
          <template slot-scope="scope">
            <el-switch :disabled="!$parent.platform.status"
                       v-model="scope.row.autosyncFlag"
                       @change="updateSelection(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page.sync="currentPage" :page-size="pageSize"
                     :total="filteredGridData.length"
                     background
                     hide-on-single-page
                     layout="total, prev, pager, next"
                     style="margin-top: 10px; text-align: center;"></el-pagination>
      <el-row style="margin-top: 10px">
        <el-button type="primary" @click="onSubmit" style="float: right">保存</el-button>
        <el-button @click="closedDialog" style="float: right; margin-right: 10px">取消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import Label from "../calculator/exportModel/Label";

export default {
  components: {Label},
  data() {
    return {
      visible: false,
      knowledgeLibs: [],
      currentPage: 1, // 第几页
      pageSize: 5,
      multipleSelection: [],
      searchInput: '',
      systemType: 'knowledge'
    }
  },
  computed: {
    filteredGridData() {
      // 计算属性必须有返回值
      let filteredGridData = []
      if (this.searchInput) {
        this.knowledgeLibs.forEach((item) => {
          if (item.knowledgeLibName.includes(this.searchInput)) {
            filteredGridData.push(item)
          }
        })
        return filteredGridData;
      } else {
        return this.knowledgeLibs;
      }
    },
  },
  methods: {
    updateSelection(row) {
      // 自动同步为true时, 默认选中;
      if (row.autosyncFlag) {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // 取消选中时, 自动更新自动改为false
      for (let item of this.knowledgeLibs) {
        if ($.inArray(item, val) == -1) {
          item.autosyncFlag = false
        }
      }
    },
    onSubmit() {
      if (this.multipleSelection.length == 0) {
        this.$message.warning("至少选择一条");
        return;
      }
      this.multipleSelection.forEach(item => {
        if (UTIL.isEmpty(item.systemType)) {
          this.$message.warning("知识体系为空");
          return;
        }
      });
      let that = this
      let param = new URLSearchParams();
      param.append("platformKnowledgeLibs", JSON.stringify(this.multipleSelection));
      this.axios.post('/pmph/action/platform/insertPlatformKnowledgeLibs', param)
        .then((response) => {
          that.closedDialog()
          that.$parent.getKnowledgeLibListByPlatformId()
          this.$refs.multipleTable.clearSelection();
        })
    },
    openDialog() {
      this.systemType = 'knowledge';
      this.getKnowledgeLibs();
    },
    getKnowledgeLibs() {
        for (let item of this.$store.state.knowledgeLibList) {
          // 已有关系, 在弹窗中不再显示;
          let existKLIds = this.$parent.platformKnowledgeLibs.map(pitem => pitem.knowledgeLibId);
          if ($.inArray(item.id, existKLIds) != -1) {
            continue
          }
          this.knowledgeLibs.push({
            knowledgeLibId: item.id,
            knowledgeLibName: item.name,
            platformId: this.$parent.platform.id,
            autosyncFlag: false,
            systemType: this.systemType
          })
        }
    },
    getCalcLibs() {
      // 已有关系, 在弹窗中不再显示;
      let existKLIds = this.$parent.platformKnowledgeLibs.map(pitem => pitem.knowledgeLibId);
      if ($.inArray(CONST.KNOWLEDGE_LIB.CALCULATOR_LIB_ID, existKLIds) != -1) {
        return;
      }
      this.knowledgeLibs.push({
        knowledgeLibId: CONST.KNOWLEDGE_LIB.CALCULATOR_LIB_ID,
        knowledgeLibName: '医学计算器',
        platformId: this.$parent.platform.id,
        autosyncFlag: false,
        systemType: this.systemType
      })
    },
    getBookLibs() {
      // 已有关系, 在弹窗中不再显示;
      let existKLIds = this.$parent.platformKnowledgeLibs.map(pitem => pitem.knowledgeLibId);
      if ($.inArray(CONST.KNOWLEDGE_LIB.BOOK_LIB_ID, existKLIds) != -1) {
        return;
      }
      this.knowledgeLibs.push({
        knowledgeLibId: CONST.KNOWLEDGE_LIB.BOOK_LIB_ID,
        knowledgeLibName: '图书',
        platformId: this.$parent.platform.id,
        autosyncFlag: false,
        systemType: this.systemType
      })
    },
    getJournalLibs() {
      this.axios.get('/pmph/action/journal/selectJournalLibs').then((response) => {
        for (let item of response.data) {
          // 已有关系, 在弹窗中不再显示;
          let existKLIds = this.$parent.platformKnowledgeLibs.map(pitem => pitem.knowledgeLibId);
          if ($.inArray(item.issn, existKLIds) != -1) {
            continue
          }
          this.knowledgeLibs.push({
            knowledgeLibId: item.issn,
            knowledgeLibName: item.name,
            platformId: this.$parent.platform.id,
            autosyncFlag: false,
            systemType: this.systemType
          })
        }
      })
    },
    getTermLibs() {
      // 已有关系, 在弹窗中不再显示;
      let existKLIds = this.$parent.platformKnowledgeLibs.map(pitem => pitem.knowledgeLibId);
      if ($.inArray(CONST.KNOWLEDGE_LIB.TERM_LIB_ID, existKLIds) != -1) {
        return;
      }
      this.knowledgeLibs.push({
        knowledgeLibId: CONST.KNOWLEDGE_LIB.TERM_LIB_ID,
        knowledgeLibName: '术语',
        platformId: this.$parent.platform.id,
        autosyncFlag: false,
        systemType: this.systemType
      })
    },
    getFlowchartLibs() {
      // 已有关系, 在弹窗中不再显示;
      let existKLIds = this.$parent.platformKnowledgeLibs.map(pitem => pitem.knowledgeLibId);
      if ($.inArray(CONST.KNOWLEDGE_LIB.FLOW_CHART_LIB_ID, existKLIds) != -1) {
        return;
      }
      this.knowledgeLibs.push({
        knowledgeLibId: CONST.KNOWLEDGE_LIB.FLOW_CHART_LIB_ID,
        knowledgeLibName: '流程图',
        platformId: this.$parent.platform.id,
        autosyncFlag: false,
        systemType: this.systemType
      })
    },
    closedDialog() {
      this.visible = false;
      this.knowledgeLibs = [];
      this.multipleSelection = [];
      this.searchInput = '';
      this.systemType = '';
    },
    valueChange(value) {
      this.$refs.multipleTable.clearSelection();
      this.knowledgeLibs = [];
      this.systemType = value;
      if (CONST.SYSTEM.KNOWLEDGE_LIB === value) {
        this.getKnowledgeLibs();
      } else if (CONST.SYSTEM.CALCULATOR === value) {
        this.getCalcLibs();
      } else if (CONST.SYSTEM.JOURNAL === value) {
        this.getJournalLibs();
      } else if (CONST.SYSTEM.BOOK === value) {
        this.getBookLibs();
      } else if (CONST.SYSTEM.TERM === value) {
        this.getTermLibs();
      } else if (CONST.SYSTEM.FLOWCHART === value) {
        this.getFlowchartLibs();
      }
    }
  },
}
</script>
<style>
#insertPKL .el-dialog__body {
  padding-top: 0px;
}
</style>
